import { Component, OnInit } from '@angular/core';
import { ActivatedRoute,Router } from '@angular/router';
import { WeibaoService } from "../weibao.service";
@Component({
  selector: 'app-weibao-confirm',
  templateUrl: './weibao-confirm.component.html',
  styleUrls: ['./weibao-confirm.component.css',
    '../../../../../assets/css/timeline.css']
})
export class WeibaoConfirmComponent implements OnInit {


  weibaoId:string = '';
  username:string = '';
  data:any;
  detailData:any;
  des:string = '';
  daochang:string = '';
  deid = {};
  constructor(private activeRoute:ActivatedRoute,
              private weibaoService:WeibaoService) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(
      params=> {
        this.weibaoId = this.weibaoService.weibaoId = params['weibaoId'];
        this.username = this.weibaoService.username;
        this.weibaoService.getData().subscribe(res => {
          this.data = res.result;
        }, err => console.log(err));

        this.weibaoService.getMans().subscribe(res => {
          this.detailData = res.result;
        }, err => console.log(err));
      }
    );
  }

  fn($event,id){
    let checkbox = $event.target;
    if (checkbox.checked) {
      this.deid[id] = id;
    }else{
      delete this.deid[id];
    }
  }

  save(){
    let didstr="";
    let dids = Object.keys(this.deid);
    for(let i=0;i<dids.length;i++){
      didstr += dids[i];
      if(dids.length!=(i+1)){
        didstr +=",";
      }
    }
    if(this.daochang == ""){
      alert('请选择是否到场！')
    }

    this.weibaoService.save({
      weibao_id:this.weibaoId.replace('|','/'),
      daochang:this.daochang,
      content:this.des,
      wb_rys:didstr
    }).subscribe(res => {
      alert("确认成功！！");
      this.weibaoService.cancel();
    },err => console.log(err));
  }

  cancel(){
    this.weibaoService.cancel();
  }

}
